import numpy as np
import re
import matplotlib.pyplot as plt

filename = 'D:/Research/2020ContrastiveLearningForSceneLabel/Code/Python/Scripts/fuck.txt'

losses = []
tmp = []
cnt = 0
with open(filename) as f:
    lines = f.readlines()
    for line in lines:
        match = re.match('.*loss \d+\.\d+ \((\d+\.\d+)\)', line)
        if match != None:
            cnt += 1
            tmp.append(float(match.group(1)))
            if cnt == 83:
                cnt = 0
                losses.append(float(match.group(1)))
                tmp = []

fig = plt.figure()
ax = fig.add_subplot(111)
idx = losses.index(min(losses))
ax.plot(losses)
ax.scatter(idx, min(losses), c = 'r')
ax.annotate('min : epoch %d, loss %.3f'%(idx + 1, min(losses)), xy=(idx, min(losses)))

# plt.savefig(filename[0:-4]+'.pdf')
plt.show()